package common

import (
	"gitee.com/zoneaurora/shop/internal/model"
	"github.com/spf13/viper"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var (
	db *gorm.DB
)

func InitDB() {
	connectArgs := viper.GetString("DataSource")
	var err error
	db, err = gorm.Open(mysql.Open(connectArgs), &gorm.Config{})
	if err != nil {
		panic("数据库连接异常")
	}
	db.AutoMigrate(&model.AdminUser{})
	db.AutoMigrate(&model.User{})
	db.AutoMigrate(&model.Category{})
	db.AutoMigrate(&model.Product{})
	db.AutoMigrate(&model.Order{})
	db.AutoMigrate(&model.OrderItem{})
}

func DB() *gorm.DB {
	return db
}
